<template>
  <div class="app-container">
<!--    <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-search"-->
<!--          @click="open1"-->
<!--          size="mini"-->
<!--          >人员选择</el-button-->
<!--        >-->
<!--        <PeopleSelect-->
<!--          ref="peopleSelect"-->
<!--          :type="'multiple'"-->
<!--          :isCheck="true"-->
<!--          :open="peopleOpen"-->
<!--          @cancel="peopleOpen = false"-->
<!--          @submit="submitPeople"-->
<!--        ></PeopleSelect>-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-search"-->
<!--          @click="open2"-->
<!--          size="mini"-->
<!--          >部门选择</el-button-->
<!--        >-->
<!--        <DepartmentDialog-->
<!--          ref="peopleSelect"-->
<!--          :type="'multiple'"-->
<!--          :isCheck="true"-->
<!--          :open="peopleOpen1"-->
<!--          @cancel="peopleOpen1 = false"-->
<!--          @submit="submitPeople1"-->
<!--        ></DepartmentDialog>-->
    <el-form
      :model="queryParamsAll"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      class="el-forms"
    >
      <div class="handle_box_content" ref="handle_box_content">
        <!-- <el-form-item label="外键" prop="refId">
        <el-input
          v-model="queryParams.refId"
          placeholder="请输入外键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <el-form-item label="评审项目" prop="reviewProject">
          <el-input
            v-model="queryParamsAll.reviewProject"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="评审类型" prop="reviewClassify">
          <!-- <el-input
            v-model="queryParamsAll.reviewClassify"
            placeholder="请选择评审类型"
            clearable
            @keyup.enter.native="handleQuery"
          /> -->
          <el-select
            v-model="queryParamsAll.reviewClassify"
            class="full-width-input"
            clearable
          >
            <el-option
              v-for="dict in dict.type.sys_review_classify"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="类别" prop="SORT">
        <el-input
          v-model="queryParams.SORT"
          placeholder="请输入类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <!-- <el-form-item label="批次编号" prop="batchCode">
        <el-input
          v-model="queryParams.batchCode"
          placeholder="请输入批次编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <el-form-item label="编码" prop="reviewCode">
          <el-input
            v-model="queryParamsAll.reviewCode"
            placeholder="请输入编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="种类序列号" prop="kindNumber">
        <el-input
          v-model="queryParams.kindNumber"
          placeholder="请输入种类序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
<!--        <el-form-item label="评审状态" prop="reviewStatus">
          <el-input
            v-model="queryParamsAll.reviewStatus"
            placeholder="请输入评审状态"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否控制点" prop="isControlPoint">
          <el-input
            v-model="queryParamsAll.isControlPoint"
            placeholder="请选择"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item> -->
        <!-- <el-form-item label="管理责任人" prop="reviewClassify">
          <el-input
            v-model="queryParams.reviewClassify"
            placeholder="请输入管理责任人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="检验结果" prop="results">
          <el-input
            v-model="queryParams.results"
            placeholder="请输入检验结果"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="检验日期" prop="inspectDate">
        <el-date-picker clearable
                        v-model="queryParams.inspectDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择检验日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="下次检验日期" prop="nextInspectDate">
        <el-date-picker clearable
                        v-model="queryParams.nextInspectDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下次检验日期">
        </el-date-picker>
      </el-form-item> -->
        <!-- <el-form-item label="备注" prop="REMARK">
        <el-input
          v-model="queryParams.REMARK"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      </div>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
<!--        <el-button
          plain
          size="mini"
          :icon="Showbox ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
          @click="handleShow"
          >{{ Showbox ? "隐藏" : "展开" }}</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:basis:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdds"
          v-hasPermi="['system:basis:add']"
          >新增子记录</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:basis:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:basis:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:basis:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      ref="tables"
      v-if="refreshTable"
      v-loading="loading"
      :data="menuList"
      stripe
      row-key="id"
      :default-expand-all="true"
      @row-click="rowClick"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange"
      @select-all="selectAll"
      @select="select"
      >
      <el-table-column type="selection" width="55" />
      <el-table-column label="评审项目" align="left" width="330" sortable>
        <template slot-scope="scope">
          <i v-if="scope.row.children == undefined" style="font-size: 12px;" class="el-icon-arrow-right"></i> {{ scope.row.reviewProject }}
        </template>
      </el-table-column>
      <el-table-column label="评审类型" align="left" prop="reviewClassify" sortable/>
      <el-table-column label="评审标准" align="left" prop="reviewStandard" />
      <el-table-column label="评审标准分" align="left" prop="reviewStandardScore" />
      <el-table-column label="是否控制点" align="left" prop="isControlPoint" />
      <el-table-column label="备注" align="left" prop="remark" />
      <el-table-column label="附件信息" align="left" prop="" />
      <el-table-column
        label="操作"
        align="center"
        width="150"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:basis:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:basis:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
<!--    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->
  </div>
</template>

<script>
import {
  listBasis,
  getBasis,
  delBasis,
  listBasisAll,
  addBasis,
  updateBasis,
} from "../../../api/system/review/basis";
import PeopleSelect from "../../../components/Selection/index.vue"
import DepartmentDialog from "../../../components/DepartmentDialog/index.vue"
export default {
  name: "Manger",
  dicts: ["sys_normal_disable","sys_review_classify"],
  components: {
    PeopleSelect,
    DepartmentDialog
  },
  data() {
    return {
      menuList:[],
      refreshTable:true,
      // 按钮loading
      // buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工器具管理表格数据
      basisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      // queryParams: {
      //   pageNum: 1,
      //   pageSize: 10,
      //   reviewClassify: undefined,
      //   reviewCode: undefined,
      //   reviewProject: undefined,
      //   reviewDate: undefined,
      //   reviewPerson: undefined,
      //   reviewPersonId: undefined,
      //   reviewObjInformation: undefined,
      //   reviewObjective: undefined,
      //   reviewStandard: undefined,
      //   reviewStandardScore: undefined,
      //   reviewOption: undefined,
      //   reviewBasic: undefined,
      //   reviewEndScore: undefined,
      //   reviewStatus: undefined,
      //   isControlPoint: undefined,
      //   remark: undefined,
      // },
      queryParamsAll: {
        reviewClassify: undefined,
        reviewCode: undefined,
        reviewProject: undefined,
        reviewDate: undefined,
        reviewPerson: undefined,
        reviewPersonId: undefined,
        reviewObjInformation: undefined,
        reviewObjective: undefined,
        reviewStandard: undefined,
        reviewStandardScore: undefined,
        reviewOption: undefined,
        reviewBasic: undefined,
        reviewEndScore: undefined,
        reviewStatus: undefined,
        isControlPoint: undefined,
        remark: undefined,
        parentId: 0,
      },
      SORTOptions: [
        {
          label: "体系评审",
          value: "体系评审",
        },{
          label: "标准化评审",
          value: "标准化评审",
        },{
          label: "应急能力建设评估",
          value: "应急能力建设评估",
        },{
          label: "安全性评价",
          value: "安全性评价",
        },{
          label: "责任制落实",
          value: "责任制落实",
        },{
          label: "基建责任制落实",
          value: "基建责任制落实",
        },{
          label: "班组评审",
          value: "班组评审",
        },{
          label: "其他",
          value: "其他",
        },{
          label: "工作落实量化处理",
          value: "工作落实量化处理",
        }
      ],
      // 表单参数
      form: {},
      Showbox:false,
      peopleOpen:false,
      peopleOpen1:false,
      // 表单校验
      rules: {
        ID: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
        refId: [{ required: true, message: "外键不能为空", trigger: "blur" }],
        projectId: [
          { required: true, message: "项目id不能为空", trigger: "blur" },
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        SORT: [{ required: true, message: "类别不能为空", trigger: "blur" }],
        batchCode: [
          { required: true, message: "批次编号不能为空", trigger: "blur" },
        ],
        toolName: [
          { required: true, message: "器具名称不能为空", trigger: "blur" },
        ],
        kindNumber: [
          { required: true, message: "种类序列号不能为空", trigger: "blur" },
        ],
        TYPE: [{ required: true, message: "型号不能为空", trigger: "change" }],
        FACTORY: [
          { required: true, message: "制造厂家不能为空", trigger: "blur" },
        ],
        custodyPerson: [
          { required: true, message: "保管负责人不能为空", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        managePerson: [
          { required: true, message: "管理责任人不能为空", trigger: "blur" },
        ],
        RESULTS: [
          { required: true, message: "检验结果不能为空", trigger: "blur" },
        ],
        inspectDate: [
          { required: true, message: "检验日期不能为空", trigger: "blur" },
        ],
        nextInspectDate: [
          { required: true, message: "下次检验日期不能为空", trigger: "blur" },
        ],
        REMARK: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        FILE: [
          { required: true, message: "附件信息不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selectAll(selection) {
      if (selection.length === this.menuList.length) {
        let rows = [];
        this.getChildren(rows, this.menuList);
        rows.forEach((row) => {
          this.$refs.tables.toggleRowSelection(row, true);
        });
      } else {
        let rows = [];
        this.getChildren(rows, this.menuList);
        rows.forEach((row) => {
          this.$refs.tables.toggleRowSelection(row, false);
        });
      }
    },
    select(selection, row) {
      let rows = [];
      this.getChildren(rows, row.children);
      rows.forEach((childRow) => {
        this.$refs.tables.toggleRowSelection(childRow);
      });
    },
    getChildren(rows, children) {
      if (children) {
        children.forEach((child) => {
          rows.push(child);
          this.getChildren(rows, child.children);
        });
      }
    },
    open1() {
      this.peopleOpen = true;
    },
    open2() {
      this.peopleOpen1 = true;
    },
    //选择人的确定按钮事件 submitPeople(nikeNamelist)方法传参一个默认接收用户昵称数组   submitPeople(peopleList,nikeNamelist)方法传参两个则是接收用户昵称数组和用户账号数组
    submitPeople(peopleList) {
      //console.log(peopleList);
      this.peopleOpen = false;
    },
    submitPeople1(peopleList) {
      //console.log(peopleList);
      this.peopleOpen1 = false;
    },
     handleRowClick(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    rowClick(row){
      this.queryParamsAll.parentId = row.id;
      this.getList(row);
    },
    handleShow() {
      this.Showbox = !this.Showbox
      const element = this.$refs.handle_box_content;
      if(this.Showbox){
        element.style.overflow = 'visible';
        element.style.height = 'fit-content'
      }else {
        element.style.overflow = 'hidden';
        element.style.height = '50px'
      }
    },
    /** 查询工器具管理列表 */
    getList(row) {
      this.loading = true;
      // console.log(row.id);
      // listBasis(this.queryParams).then((response) => {
      //   this.menuList = this.handleTree(response.rows, "id");
      //   console.log(response);
      //   console.log(this.menuList);
      //   this.basisList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      listBasisAll(this.queryParamsAll).then((response) => {
        if(row != "" && row != undefined){
          console.log(row.children);
          row.children = (this.handleTree(response, "id"))
        }else{
          this.menuList = this.handleTree(response, "id");
        }
        // debugger

        console.log(this.menuList);
        this.basisList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: undefined,
        refId: undefined,
        projectId: undefined,
        projectName: undefined,
        SORT: undefined,
        batchCode: undefined,
        toolName: undefined,
        kindNumber: undefined,
        TYPE: undefined,
        FACTORY: undefined,
        custodyPerson: undefined,
        startTime: undefined,
        endTime: undefined,
        managePerson: undefined,
        RESULTS: undefined,
        inspectDate: undefined,
        nextInspectDate: undefined,
        REMARK: undefined,
        FILE: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParamsAll.parentId = 0;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      debugger
      this.ids = []; // 重置选中的ids
      this.flattenSelection(selection); // 调用递归函数
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      console.log(this.ids);
    },
    flattenSelection(nodes) {
      nodes.forEach(node => {
        this.ids.push(node.id); // 收集当前节点的id
        if (node.children && node.children.length) {
          this.flattenSelection(node.children); // 递归处理子节点
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: "/review/basicdata/basicDataForm",
        query: {title:"新增评审基础数据" },
      });
    },
    /** 新增子记录操作 */
    handleAdds(row) {
      // this.loading = true;
      this.reset();
      const ID = row.id || this.ids;
      getBasis(ID).then((response) => {
        // this.loading = false;
        // this.form = response.data;
        // this.open = true;
        // this.title = "修改工器具管理";
      });

      this.$router.push({
        path: "/review/basicdata/basicDataForm",
        query: { ID:ID,title:"新增评审基础数据",flowId:2 },
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.loading = true;
      this.reset();
      const ID = row.id || this.ids;
      getBasis(ID).then((response) => {
        // this.loading = false;
        // this.form = response.data;
        // this.open = true;
        // this.title = "修改工器具管理";
      });

      this.$router.push({
        path: "/review/basicdata/basicDataForm",
        query: { ID:ID,title:"修改评审基础数据",flowId:1 },
      });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate((valid) => {
    //     if (valid) {
    //       // this.buttonLoading = true;
    //       if (this.form.ID != null) {
    //         updateBasis(this.form)
    //           .then((response) => {
    //             this.$modal.msgSuccess("修改成功");
    //             this.open = false;
    //             this.getList();
    //           })
    //           .finally(() => {
    //             // this.buttonLoading = false;
    //           });
    //       } else {
    //         addBasis(this.form)
    //           .then((response) => {
    //             this.$modal.msgSuccess("新增成功");
    //             this.open = false;
    //             this.getList();
    //           })
    //           .finally(() => {
    //             // this.buttonLoading = false;
    //           });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除工器具管理编号为"' + IDs + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBasis(IDs);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/basis/export",
        {
          ...this.queryParams,
        },
        `basis_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
.handle_box_content {
  width: 81%;
  height: 50px;
  overflow: hidden;
}
.el-forms{
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.tableNer ::after{
  content: "1";
}
</style>
